package com.managementemployees.database;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.managementemployees.model.NhanVien;
import com.managementemployees.utils.Constant;

public class NhanVienHelper extends DatabaseHelper {

	String TAG = "NhanVienHelper";

	public NhanVienHelper(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	// add nhanvien
	public void addNhanVien(NhanVien nv) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();

		values.put(Constant.KEY_TEN, nv.getTenNV());
		values.put(Constant.KEY_GIOITINH, nv.getGioiTinh());
		values.put(Constant.KEY_NGAYSINH, nv.getNgaySinh());
		values.put(Constant.KEY_ID_CONGVIEC, nv.getIdLoaiCongViec());

		db.insert(Constant.TABLE_NHANVIEN, null, values);
		db.close();
		Log.d(TAG, "addNhanVien " + nv.toString());
	}

	// get nhan vien
	public NhanVien getNhanVien(int id) {
		Log.d(TAG, "getNhanVien id:" + id);
		SQLiteDatabase db = this.getReadableDatabase();
		NhanVien nv = null;
		String select = "SELECT * FROM " + Constant.TABLE_NHANVIEN + " WHERE "
				+ Constant.KEY_ID + "=" + id;
		Cursor cursor = db.rawQuery(select, null);

		if (cursor != null)
			cursor.moveToFirst();

		Log.d(TAG, "HungND cursor :" + cursor.getCount());
		nv = new NhanVien();
		nv.setId(Integer.parseInt(cursor.getString(0)));
		nv.setTenNV(cursor.getString(1));
		nv.setGioiTinh(cursor.getString(2));
		nv.setNgaySinh(cursor.getString(3));
		nv.setIdLoaiCongViec(Integer.parseInt(cursor.getString(4)));

		Log.d(TAG, "getNhanVien " + nv.toString());
		return nv;
	}

	// get all nhan vien ASC
	public ArrayList<NhanVien> getAllNhanVien() {
		ArrayList<NhanVien> arrayList = new ArrayList<>();
		SQLiteDatabase db = this.getWritableDatabase();
		NhanVien nv;
		Cursor cursor = db.rawQuery("SELECT * FROM " + Constant.TABLE_NHANVIEN,
				null);
		if (cursor.moveToFirst()) {
			do {
				nv = new NhanVien();
				nv.setId(Integer.parseInt(cursor.getString(0)));
				nv.setTenNV(cursor.getString(1));
				nv.setGioiTinh(cursor.getString(2));
				nv.setNgaySinh(cursor.getString(3));
				nv.setIdLoaiCongViec(Integer.parseInt(cursor.getString(4)));
				arrayList.add(nv);
			} while (cursor.moveToNext());
		}
		db.close();
		Log.d(TAG, "getAllNhanVien size : " + arrayList.size());
		return arrayList;
	}

	// update nhanvien
	public int updateNhanVien(NhanVien nv) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();

		values.put(Constant.KEY_TEN, nv.getTenNV());
		values.put(Constant.KEY_GIOITINH, nv.getGioiTinh());
		values.put(Constant.KEY_NGAYSINH, nv.getNgaySinh());
		values.put(Constant.KEY_ID_CONGVIEC, nv.getIdLoaiCongViec());
		int i = db.update(Constant.TABLE_NHANVIEN, values,
				Constant.KEY_ID = "?",
				new String[] { String.valueOf(nv.getId()) });
		Log.d(TAG, "updateNhanVien id :" + nv.getId());
		return i;
	}

	// del nhan vien
	public void deleteNhanVien(NhanVien nv) {
		SQLiteDatabase db = this.getWritableDatabase();
		int i = db.delete(Constant.TABLE_NHANVIEN, Constant.KEY_ID + " =?",
				new String[] { String.valueOf(nv.getId()) });
		db.close();

	}
}
